
capture log close

set more off
set matsize 10000

use rep_final, clear

	*noncitizens
	keep if cit==0
	*married last year, currently still married with spouse present
	keep if marrinyr==2 & married==1 
	*spouse info is avail.
	drop if citizen_sp==.

* age of marriage

	g agemarried= age if yrmarr==year
	replace agemarried= age - 1 if yrmarr<year

* Spouse age of marriage
	g agemarried_sp = age_sp if yrmarr==year
	replace agemarried_sp = age_sp - 1 if yrmarr<year
	
* Define control variables 

	local demog "age age2 male yreduc i.racegr"
	
	local demog1 "yreduc i.racegr"
	local demog2 "age age2 i.racegr"
	
	local imm "ysm i.bpl " 
	local fe "i.statefip i.yrmarr "
	
	
	local full "`demog' `imm' `fe' "

	
	g post1=(yrmarr>=2013 & yrmarr<=2016)
	g post2=(yrmarr>=2017)
	
	g agemarrieddiff=agemarried-agemarried_sp

	g dacacitsp=treatgr*cit_sp
	g citsppost1=cit_sp*post1
	g citsppost2=cit_sp*post2
	g treatpost1=treatgr*post1
	g treatpost2=treatgr*post2
	g treatcitsppost1=treatgr*cit_sp*post1
	g treatcitsppost2=treatgr*cit_sp*post2	

	
* age,educ, earnings of daca and their spouses with citizen spouse, compared to control group, by gender

	local a=1

 	foreach gender of varlist male female{
	
	preserve
		keep if `gender'==1
			
			reg agemarrieddiff treatgr cit_sp dacacitsp citsppost1 citsppost2 ///
						treatpost1 treatpost2 treatcitsppost1 treatcitsppost2 ///
						`full' if daca==1|controlgrdaca==1  [pweight = perwt] , cluster(statefip)
			est sto b`a'1
			
			reg educdiff treatgr cit_sp dacacitsp citsppost1 citsppost2 ///
						treatpost1 treatpost2 treatcitsppost1 treatcitsppost2 ///
						`full' if daca==1|controlgrdaca==1  [pweight = perwt] , cluster(statefip)
			est sto b`a'2	

			local a=`a'+1
			
			*dep var mean
				sum agemarrieddiff educdiff [fweight=perwt] if e(sample) 
					
	restore	
	}
			*Make table 
			esttab b11 b12 b21 b22  ///
					using tableA7.csv, se  ///
					title(Intermarriage Rate) ///
					b(%9.4f) se(%9.4f) star(* 0.1 ** 0.05 *** 0.01) ///
					nogaps replace
